package com.tsing.bms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.bms.dto.NameAndPhoneDto;
import com.tsing.bms.entity.Carport;
import com.tsing.bms.mapper.CarportMapper;
import com.tsing.bms.service.ICarportService;
import com.tsing.bms.vo.req.*;
import com.tsing.bms.vo.resp.CarportRageRespVo;
import com.tsing.bms.vo.resp.CarportRespVo;
import com.tsing.common.exceptions.BusinessException;
import com.tsing.common.exceptions.ServerException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
@Service
public class CarportServiceImpl extends ServiceImpl<CarportMapper, Carport> implements ICarportService {

    @Override
    public void addCarport(CarportAddReqVo reqVo) {
//        if (0 != baseMapper.selectCount(new QueryWrapper<Carport>().eq("name", reqVo.getName()))) {
//            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
//        }
        Carport carport = new Carport();
        BeanUtils.copyProperties(reqVo, carport);
        baseMapper.insert(carport);
    }

    @Override
    public void deleteCarport(IdsReqVo reqVo) {
        if (!StringUtils.isBlank(baseMapper.haveRelevance(reqVo.getIds()))) {
            throw new BusinessException(ServerException.Proxy.CARPORT_HAVE_RELEVANCE);
        }
        baseMapper.deleteBatchIds(reqVo.getIds());
    }

    @Override
    public CarportRespVo getCarport(IdReqVo reqVo) {
        CarportRespVo carportRespVo = new CarportRespVo();
        BeanUtils.copyProperties(baseMapper.selectById(reqVo.getId()), carportRespVo);
        return carportRespVo;
    }

    @Override
    public void updateCarport(CarportUpdateReqVo reqVo) {
//        if (0 != baseMapper.selectCount(new QueryWrapper<Carport>().eq("name", reqVo.getName())
//                .ne("id", reqVo.getId()))) {
//            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
//        }
        Carport carport = new Carport();
        BeanUtils.copyProperties(reqVo, carport);
        baseMapper.updateById(carport);
    }

    @Override
    public Page<CarportRageRespVo> carportPage(CarportPageReqVo reqVo) {
        Page<Carport> page = new Page<>(reqVo.getPageNum(), reqVo.getPageSize());
        List<CarportRageRespVo> list = baseMapper.carportPage(reqVo, page);
        list.forEach(e -> {
            if (StrUtil.isNotBlank(e.getHouseholdId())) {
                NameAndPhoneDto nameAndPhoneDto = baseMapper.selectZhuhu(e.getHouseholdId());
                e.setHouseholdName(nameAndPhoneDto.getName());
                e.setHouseholdPhone(nameAndPhoneDto.getPhone());
            }
        });
        Page<CarportRageRespVo> carportPage = new Page<>();
        carportPage.setTotal(page.getTotal());//总条数
        carportPage.setCurrent(page.getCurrent());//页码
        carportPage.setSize(page.getSize());//当前页个数
        carportPage.setRecords(list);//当前页数据
        return carportPage;
    }
}
